$(document).ready(function(){
    $('body').append('<div id="dialog-confirm"></div>');
    
    $("#dialog-confirm").dialog({
        autoOpen:false,
        resizable:false,
        height:0,
        modal:true
//        buttons:{
//            'Confirm': function(){
//                var ids =jQuery("#list10").jqGrid('getGridParam','selarrrow');
//                $.ajax({
//                    type: "POST",
//                    url: "url&names="+ids,
//                    data: JSON.stringify(ids), 
//                    dataType: "json"
//                });
//            },
//            'cancel': function(){
//                $(this).dialog('close');
//            }
//        }
    });
    
    dialogLoading = function() {
        $("#dialog-confirm").html('');
        $("#dialog-confirm").dialog({
            autoOpen:true,
            open: function(event, ui) { 
                $('.ui-dialog-titlebar-close').hide()
            },
            title:'Loading ...',
            buttons:[],
            height:0,
            position: "center"
        });
    }
    
    $('.action-edit').live('click',function(){
        dialogLoading();
        var url = $(this).attr('url');
        var title = $(this).attr('title');
        $.ajax({
            url:url,
            type:'get',
            success:function(data){
                $("#dialog-confirm").dialog({height:'auto',title:title,
                    buttons:[
                        {
                            text: "Save",
                            click: function() { 
                                data = $(this).children('form').serialize();
                                dialogLoading();
                                $.ajax({
                                    url:url,
                                    data:data,
                                    type:'post',
                                    success:function(data){
                                        try {
                                            json = jQuery.parseJSON(data);
                                            if(json.success == true) {
                                                alert('success');
                                            }
                                        }catch(e) {
                                            $("#dialog-confirm").html(data);
                                        }
                                    },
                                    error:function(e){
                                        
                                    }
                                });
                            }
                        },
                        {
                            text: "Cancel",
                            click: function() { $(this).dialog("close"); }
                        }
                    ]});
                $('#dialog-confirm').html(data);
                $("#dialog-confirm").dialog({position: "center"});
            },
            error:function(data){
                
            }
        })
    });
    
    
    
    $('.action-view').live('click',function(){
        $("#dialog-confirm").html('');
        // restore
        $("#dialog-confirm").dialog({buttons:[],height:0,position: "center"});
        $("#dialog-confirm").dialog({
            autoOpen:true,
            open: function(event, ui) { 
                $('.ui-dialog-titlebar-close').hide()
            },
            title:'Loading ...'
        });
        
        var url = $(this).attr('url');
        var title = $(this).attr('title');
        $.ajax({
            url:url,
            type:'get',
            success:function(data){
                $("#dialog-confirm").dialog({height:'auto',title:title,buttons:[
                    {
                    text: "Ok",
                    click: function() { $(this).dialog("close"); }
                    }
                ]});
                $('#dialog-confirm').html(data);
                $("#dialog-confirm").dialog({position: "center"});
            },
            error:function(data){
                
            }
        })
    });
});


